from flask import *
from flask_restful import Api, Resource, reqparse
from models.model import *
from views.view import login

issue_bp = Blueprint('issue_bp', __name__)
api = Api(issue_bp)


# 获取单个问题
class IssueOneView(Resource):
    def get(self):
        id = request.args.get('id')
        issue_info = IssueModel.query.get(id)
        if issue_info.user.img == '1':
            img = 'https://www.com8.cn/wp-content/uploads/2020/09/20200922022838-5f6961562471f.jpg'
        else:
            img = current_app.config.get('QINIU_HOST') + issue_info.user.img
        print(issue_info.user.img)
        issue_data = {
            'id': issue_info.id,
            'name': issue_info.name,
            'title': issue_info.title,
            'upload_time': str(issue_info.upload_time).split()[0],
            'img': img,
            'username': issue_info.user.name,
            'remark': len(issue_info.remark)
        }
        return jsonify({'code': 200, 'list': issue_data})
api.add_resource(IssueOneView, '/issue/one')


# 问题视图
class IssueView(Resource):
    def get(self):
        issue_all = IssueModel.query.all()
        issue_list = []
        for item in issue_all:
            if item.user.img == '1':
                img = 'https://www.com8.cn/wp-content/uploads/2020/09/20200922022838-5f6961562471f.jpg'
            else:
                img = current_app.config.get('QINIU_HOST') + item.user.img
            print(item.user.img)
            issue_list.append({
                'id': item.id,
                'name': item.name,
                'title': item.title,
                'upload_time': str(item.upload_time),
                'img': img,
                'username': item.user.name
            })
        return jsonify({'code': 200, 'list': issue_list})

    @login
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name', required=True)
        req.add_argument('title', required=True)
        args = req.parse_args()
        print('}}}}', g.id)
        # 添加数据
        try:
            db.session.add(IssueModel(
                name=args['name'],
                title=args['title'],
                user_id=g.id,
            ))
            db.session.commit()
            return jsonify({'code': 200, 'msg': "发送成功"})
        except Exception as e:
            print(e)
            return jsonify({'code': 400, 'msg': "发送失败"})

api.add_resource(IssueView, '/issue')


# 评论
class RemarkView(Resource):
    def get(self):
        id = request.args.get('id')
        issue_info = IssueModel.query.get(id)
        print("]]]]]", g.id)

        remark_list = []
        status_list = []
        for i in issue_info.remark:
            ser = PriseModel.query.filter(PriseModel.remark_id == i.id).filter(
                PriseModel.user_id == g.id).first()
            print('---------{{{{{', ser, '}}}}}}}}------')
            if ser:
                status = True
            else:
                status = False
            status_list.append(status)
        print('+++++++++>', status_list)

        number = 0
        for item in issue_info.remark:
            if item.user.img == '1':
                img = 'https://www.com8.cn/wp-content/uploads/2020/09/20200922022838-5f6961562471f.jpg'
            else:
                img = current_app.config.get('QINIU_HOST') + item.user.img



            remark_list.append({
                'id': item.id,
                'title': item.title,
                'upload_time': str(item.upload_time).split()[1],
                'prise': len(item.prise),
                'user': {
                    'name': item.user.name,
                    'img': img,
                    'status': status_list[number]
                },
            })
            number += 1

        return jsonify({'code': 200, 'list': remark_list})

    @login
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('title', required=True)
        req.add_argument('id', type=int, required=True)
        args = req.parse_args()

        try:
            db.session.add(RemarkModel(
                title=args['title'],
                issue_id=args['id'],
                user_id=g.id,
            ))
            db.session.commit()
            return jsonify({'code': 200, 'msg': '发布成功'})
        except Exception as e:
            print(e)
            return jsonify({'code': 400, 'msg': '发送失败'})


api.add_resource(RemarkView, '/remark')



# 点赞
class PriseView(Resource):
    def get(self):
        return '11111'

    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('id', required=True)
        args = req.parse_args()
        # 判断是否存在
        prise_info = PriseModel.query.filter(PriseModel.user_id == g.id).filter(PriseModel.remark_id == args['id']).first()
        # return jsonify({'msg':prise_info})
        if prise_info:
            PriseModel.query.filter(PriseModel.user_id == g.id).filter(PriseModel.remark_id == args['id']).delete()
            db.session.commit()
            return jsonify({'code': 204,  'msg': '已取消'})

        db.session.add(PriseModel(
            praise=True,
            user_id=g.id,
            remark_id=args['id'],
        ))
        db.session.commit()
        return jsonify({'code': 200, 'msg': '已点赞'})


api.add_resource(PriseView, '/prise')


